package psd.model.Prolog;

import java.io.BufferedWriter;
import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileWriter;
import java.io.IOException;
import java.io.PrintWriter;
import java.util.ArrayList;
import java.util.List;
import java.util.Scanner;

import psd.config.ConfigurationReader;


public class FactFileWriter implements IFactWriter {
	private PrintWriter prologWriter = null;
	private List clauses;
	public FactFileWriter(){
		InitPLFile();
		clauses = new ArrayList();
	}
	
	private void InitPLFile() {
		try {
			String path = ConfigurationReader.Instance().getProperty("databasePath");
			if(!path.isEmpty())
			{
				File f = new File(ConfigurationReader.Instance().getProperty("databasePath"));
				if(!f.exists())
					f.mkdirs();
				path += "//PSD.pl";
			}else
				path = "PSD.pl";
			FileWriter writer = new FileWriter(path);
			
			BufferedWriter bw = new BufferedWriter(writer);
			setPrologWriter(new PrintWriter(bw));
			writeConfigurablePrologFunctions();
			
		} catch (IOException e) {
			;
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
	}
	
	private void setPrologWriter(PrintWriter prologWriter) {
		this.prologWriter = prologWriter;
	}
	
	@Override
	public void writeFact(String functor, String param[])
    {	
		StringBuffer prolog_term = term(functor, param);
        //String s = term("", prolog_term.toString()).toString();
        String s = prolog_term.toString() + ".";
        clauses.add(s);
        prologWriter.println(s);
		prologWriter.flush();
		
    }
	
	public void writeConfigurablePrologFunctions() throws FileNotFoundException
	{  
		File f = new File(ConfigurationReader.Instance().getProperty("baseCommonFunctions"));
		StringBuilder text = new StringBuilder();
		String NL = System.getProperty("line.separator");
		    Scanner scanner = new Scanner(f);
		    try {
		      while (scanner.hasNextLine()){
		    	  prologWriter.println(scanner.nextLine() + NL);
		      }
		    }
		    finally{
		      scanner.close();
		    }
	}
	

    private StringBuffer term(String functor, String arg)
    {
        return term(functor, new String[] {
            arg
        });
    }

    private StringBuffer term(String functor, StringBuffer arg)
    {
        return term(functor, new StringBuffer[] {
            arg
        });
    }

    private StringBuffer term(String functor, String args[])
    {
        StringBuffer sb = new StringBuffer();
        sb.append(functor);
        sb.append('(');
        for(int i = 0; i < args.length; i++)
        {
            if(i > 0)
            {
                sb.append(", ");
            }
            sb.append(args[i]);
        }

        sb.append(')');
        return sb;
    }

    private StringBuffer term(String functor, StringBuffer args[])
    {
        StringBuffer sb = new StringBuffer();
        sb.append(functor);
        sb.append('(');
        for(int i = 0; i < args.length; i++)
        {
            if(i > 0)
            {
                sb.append(", ");
            }
            sb.append(args[i]);
        }

        sb.append(')');
        return sb;
    }
}
